package com.zhuifeng.orders.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.zhuifeng.common.core.domain.R;
import com.zhuifeng.orders.domain.OrdersEcho;
import com.zhuifeng.orders.domain.SubMitOrders;
import com.zhuifeng.product.domain.ProductRultAttrCenter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.zhuifeng.common.log.annotation.Log;
import com.zhuifeng.common.log.enums.BusinessType;
import com.zhuifeng.common.security.annotation.RequiresPermissions;
import com.zhuifeng.orders.domain.Orders;
import com.zhuifeng.orders.service.IOrdersService;
import com.zhuifeng.common.core.web.controller.BaseController;
import com.zhuifeng.common.core.web.domain.AjaxResult;
import com.zhuifeng.common.core.utils.poi.ExcelUtil;
import com.zhuifeng.common.core.web.page.TableDataInfo;

/**
 * 订单Controller...
 * 
 * @author zhuifeng
 * @date 2022-08-18
 */
@RestController
@RequestMapping("/orders")
public class OrdersController extends BaseController
{
    @Autowired
    private IOrdersService ordersService;

    /**
     * 查询订单列表
     */
    @RequiresPermissions("orders:orders:list")
    @GetMapping("/list")
    public TableDataInfo list(Orders orders)
    {
        startPage();
        List<Orders> list = ordersService.selectOrdersList(orders);
        return getDataTable(list);
    }

    /**
     * 导出订单列表
     */
    @RequiresPermissions("orders:orders:export")
    @Log(title = "订单", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, Orders orders)
    {
        List<Orders> list = ordersService.selectOrdersList(orders);
        ExcelUtil<Orders> util = new ExcelUtil<Orders>(Orders.class);
        util.exportExcel(response, list, "订单数据");
    }

    /**
     * 获取订单详细信息
     */
    @RequiresPermissions("orders:orders:query")
    @GetMapping(value = "/{ordersId}")
    public AjaxResult getInfo(@PathVariable("ordersId") String ordersId)
    {
        return AjaxResult.success(ordersService.selectOrdersByOrdersId(ordersId));
    }

    /**
     * 新增订单
     */
    @RequiresPermissions("orders:orders:add")
    @Log(title = "订单", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Orders orders)
    {
        return toAjax(ordersService.insertOrders(orders));
    }

    /**
     * 修改订单
     */
    @RequiresPermissions("orders:orders:edit")
    @Log(title = "订单", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Orders orders)
    {
        return toAjax(ordersService.updateOrders(orders));
    }

    /**
     * 删除订单
     */
    @RequiresPermissions("orders:orders:remove")
    @Log(title = "订单", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ordersIds}")
    public AjaxResult remove(@PathVariable String[] ordersIds)
    {
        return toAjax(ordersService.deleteOrdersByOrdersIds(ordersIds));
    }

    /**
     * 确认收货
     */
    @RequiresPermissions("orders:orders:confirmReceiptUpdateState")
    @Log(title = "订单", businessType = BusinessType.DELETE)
    @GetMapping("/confirmReceiptUpdateState/{ordersId}")
    public R confirmReceiptUpdateState(@PathVariable("ordersId")String ordersId){
        return this.ordersService.confirmReceiptUpdateState(ordersId);
    }

    /**
     * 获取sku编号查询信息回显
     */
    @RequiresPermissions("orders:details:ordersSelectSkuIdEcho")
    @Log(title = "订单", businessType = BusinessType.DELETE)
    @GetMapping("/ordersSelectSkuIdEcho/{productSkuId}")
    public R<OrdersEcho> ordersSelectSkuIdEcho(@PathVariable("productSkuId")String productSkuId){
        return R.ok(this.ordersService.selectProductSkuId(productSkuId));
    }

    /**
     * 生成令牌
     */
    public List<String> createToken(){
        return this.ordersService.createToken();
    }

    /**
     * 获取token
     */
    @RequiresPermissions("orders:orders:getToken")
    @Log(title = "订单", businessType = BusinessType.DELETE)
    @GetMapping("/getToken")
    public R getToken(){
        List<String> token = this.createToken();
        String tokens= this.ordersService.getToken(token);
        return R.ok(tokens);
    }

    /**
     * 订单生成
     */
    @RequiresPermissions("orders:orders:subMitOrdersCreate")
    @Log(title = "订单", businessType = BusinessType.INSERT)
    @PostMapping("/subMitOrdersCreate")
    public R subMitOrdersCreate(@RequestBody SubMitOrders subMitOrders)
    {
        return ordersService.subMitOrdersCreate(subMitOrders);
    }

    /**
     * 确认收货
     */
    @RequiresPermissions("orders:orders:affirmReceiving")
    @Log(title = "订单", businessType = BusinessType.DELETE)
    @PutMapping("/{ordersIds}")
    public AjaxResult affirmReceiving(@PathVariable String[] ordersIds)
    {
        return toAjax(ordersService.affirmReceivingOrdersByOrdersIds(ordersIds));
    }




}
